package cn.bjtu.archiprac.papercheck.application.controller;

import cn.bjtu.archiprac.papercheck.application.exception.IncorrectPwdException;
import cn.bjtu.archiprac.papercheck.application.exception.UserExistException;
import cn.bjtu.archiprac.papercheck.application.exception.UserNotFoundException;
import cn.bjtu.archiprac.papercheck.application.model.Pagination;
import cn.bjtu.archiprac.papercheck.application.vo.Response;
import cn.bjtu.archiprac.papercheck.application.constant.Constants;
import cn.bjtu.archiprac.papercheck.application.service.iface.IUserService;
import cn.bjtu.archiprac.papercheck.application.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import cn.bjtu.archiprac.papercheck.application.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Created by Zexin Lin on 4/12/2018.
 * 用户管理模块控制器
 */
@Controller
@RequestMapping(path = "/user")
public class UserController {
    final private IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/register")
    @ResponseBody
    public Response register(@RequestBody User user) throws UserExistException {
        Response response = new Response();
        userService.register(user);
        User fetchedUser = userService.queryUserInfo(user.getUserId());

        Pagination<User> data = new Pagination<User>();
        data.setList(fetchedUser);
        response.setData(data);
        response.setStatusCode(Constants.StatusCode.SUCCESS);
        response.setMsg("注册成功");
        return response;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/login")
    @ResponseBody
    public Response login(@RequestBody User user, HttpSession session) throws UserExistException, IncorrectPwdException, UserNotFoundException {
        Response response = new Response();
        User fetchedUser = null;
        if (session.getAttribute("currentUser") != null) {
            fetchedUser = (User) session.getAttribute("currentUser");
            response.setMsg("已登录");
        }
        else {
            fetchedUser = userService.login(user);
            session.setAttribute("currentUser", fetchedUser);
            response.setMsg("登陆成功");
        }

        Pagination<User> data = new Pagination<User>();
        data.setList(fetchedUser);
        response.setData(data);
        response.setStatusCode(Constants.StatusCode.SUCCESS);
        return response;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/logoff")
    @ResponseBody
    public Response logoff(HttpSession session) throws UserExistException, IncorrectPwdException, UserNotFoundException {
        Response response = new Response();
        User fetchedUser = null;
        session.removeAttribute("currentUser");
        response.setMsg("注销成功");
        response.setStatusCode(Constants.StatusCode.SUCCESS);
        return response;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/list")
    @ResponseBody
    public Response queryUserList(@RequestBody UserVO queryVO) {
        Response response = new Response();
        Pagination<User> data = new Pagination<User>(queryVO.getPageIndex(), queryVO.getPageSize());
        data.setList(userService.queryUserList(queryVO));
        response.setData(data);
        response.setStatusCode(Constants.StatusCode.SUCCESS);
        response.setMsg("查询用户列表成功");
        return response;
    }
}
